﻿Imports System.Net
Imports System.Text
Imports System.IO

Public Class GetWebPage



    Public Function HttpGet(ByVal uri As String, ByVal username As String, ByVal password As String) As String
        Dim response As String = Nothing
        Dim webClient As New WebClient
        Using webClient

            If Not (String.IsNullOrEmpty(username) OrElse String.IsNullOrEmpty(password)) Then
                webClient.Credentials = New NetworkCredential(username, password)
            End If
            Try
                Dim smr As New StreamReader(webClient.OpenRead(uri), Encoding.GetEncoding("GB2312"))
                Return smr.ReadToEnd


                '处理异常
            Catch ex As WebException
                If TypeOf ex.Response Is HttpWebResponse Then
                    Dim status_cod As HttpStatusCode = DirectCast(ex.Response, HttpWebResponse).StatusCode

                    Select Case status_cod
                        Case HttpStatusCode.Unauthorized
                            response = ""
                        Case HttpStatusCode.NotFound
                            response = ""
                        Case HttpStatusCode.Forbidden
                            response = ""
                        Case HttpStatusCode.InternalServerError
                            response = ""
                        Case HttpStatusCode.Ambiguous
                            response = ""
                        Case Else
                            response = ""
                            'Throw ex
                    End Select

                End If
                Return response
            Catch ioe As IOException
                response = ""
            End Try 

        End Using

        Return response
    End Function
End Class
